import 'package:flutter/material.dart';
import 'package:yurun_smartmerchants/global.dart';
import 'package:yurun_smartmerchants/interface/DialogInterface.dart';
import 'package:yurun_smartmerchants/models/update_app_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/DialogUtil.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/utils/cacheUtils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';

class MineSetup extends StatefulWidget {
  @override
  _MineSetPageState createState() => _MineSetPageState();
}

class _MineSetPageState extends State<MineSetup> implements DialogInterface {
  String cache;

  @override
  void initState() {
    super.initState();
    CacheUtils.loadCache().then((value) => {
          setState(() {
            cache = value;
          })
        });
  }

  void getList() {
    LoadingUtil.showLoading(context, tip: '加载中...');
    ApiInterface.updateApp().then((value) {
      UpdateAppEntity updateAppEntity = UpdateAppEntity.fromJson(value);
      if (!updateAppEntity.force) {
        showToast("当前是最新版本");
        return;
      }
      setState(() {});
    }).whenComplete(() => LoadingUtil.hideLoading(context));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          title: Text('设置'),
          backgroundColor: Colours.title_bg,
          textTheme: Theme.of(context).textTheme.apply(
                bodyColor: Colours.color_313131,
                displayColor: Colours.color_313131,
              ),
          iconTheme: IconThemeData(
            color: Colours.color_313131,
          )),
      body: Container(
        color: Colours.mine_bg,
        width: double.infinity,
        height: double.infinity,
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              GestureDetector(
                  onTap: () {
                    CacheUtils.clearCache();
                    setState(() {
                      cache = "0.00B";
                    });
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: AppSize.sp(18)),
                    height: AppSize.height(98),
                    width: double.infinity,
                    color: Colours.white,
                    child: Stack(
                      children: <Widget>[
                        Container(
                          child: Text(
                            "清除缓存",
                            style: TextStyle(
                                fontSize: AppSize.sp(32),
                                color: Colours.gray_33),
                          ),
                          padding: EdgeInsets.only(left: AppSize.sp(32)),
                          alignment: Alignment.centerLeft,
                        ),
                        Container(
                          child: Text(
                            cache == null ? "0.00B" : cache,
                            style: TextStyle(
                                fontSize: AppSize.sp(30),
                                color: Colours.color_959595),
                          ),
                          padding: EdgeInsets.only(right: AppSize.sp(32)),
                          alignment: Alignment.centerRight,
                        ),
                      ],
                    ),
                  )),
              GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, mine_setup_service_page);
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: AppSize.sp(1)),
                    height: AppSize.height(98),
                    width: double.infinity,
                    color: Colours.white,
                    child: Stack(
                      children: <Widget>[
                        Container(
                          child: Text(
                            "服务条例",
                            style: TextStyle(
                                fontSize: AppSize.sp(32),
                                color: Colours.gray_33),
                          ),
                          padding: EdgeInsets.only(left: AppSize.sp(32)),
                          alignment: Alignment.centerLeft,
                        ),
                        Container(
                          child: Image.asset(
                            'images/mine_arrow.png',
                            width: AppSize.width(11),
                            height: AppSize.height(23),
                          ),
                          padding: EdgeInsets.only(right: AppSize.sp(32)),
                          alignment: Alignment.centerRight,
                        ),
                      ],
                    ),
                  )),
              GestureDetector(
                  onTap: () {
                    getList();
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: AppSize.sp(1)),
                    height: AppSize.height(98),
                    width: double.infinity,
                    color: Colours.white,
                    child: Stack(
                      children: <Widget>[
                        Container(
                          child: Text(
                            "检查更新",
                            style: TextStyle(
                                fontSize: AppSize.sp(32),
                                color: Colours.gray_33),
                          ),
                          padding: EdgeInsets.only(left: AppSize.sp(32)),
                          alignment: Alignment.centerLeft,
                        ),
                        Container(
                          child: Image.asset(
                            'images/mine_arrow.png',
                            width: AppSize.width(11),
                            height: AppSize.height(23),
                          ),
                          padding: EdgeInsets.only(right: AppSize.sp(32)),
                          alignment: Alignment.centerRight,
                        ),
                      ],
                    ),
                  )),
              Spacer(),
              GestureDetector(
                  onTap: () {
                    DialogUtil.d.show(context, "你确定要退出吗?", "确定", false, this);
                  },
                  child: Container(
                      alignment: Alignment.bottomCenter,
                      height: AppSize.height(98),
                      width: double.infinity,
                      color: Colours.white,
                      child: Stack(
                        children: <Widget>[
                          Container(
                            child: Text(
                              "退出登录",
                              style: TextStyle(
                                  fontSize: AppSize.sp(32),
                                  color: Colours.app_main),
                            ),
                            padding: EdgeInsets.only(left: AppSize.sp(32)),
                            alignment: Alignment.center,
                          )
                        ],
                      )))
            ]),
      ),
    );
  }

  @override
  void sumit() {
    // TODO: implement sumit
    Global.prefs.clear();
    Navigator.of(context)
        .pushNamedAndRemoveUntil(login_page, ModalRoute.withName(login_page));
  }
}
